package com.etlmaster.executor.steps.zookeeper;


import org.apache.zookeeper.data.Stat;

import com.etlmaster.executor.Step;
import com.etlmaster.executor.StepMeta;
import com.etlmaster.executor.annotation.StepInstruction;
import com.etlmaster.executor.utils.DataCollectionUtils;
import com.etlmaster.executor.utils.StepState;
import com.etlmaster.executor.utils.ZKUtil;

/**
 * @author Larry
 * @copyright Larry@etlMaster
 * @since 2017-09-10
 */
@StepInstruction("createTmpZkPath")
public class CreateTmpZkPathStep extends Step{

	private CreateTmpZkPathStepMeta meta;
	public CreateTmpZkPathStep(StepMeta meta) {
		super(meta);
		this.meta=(CreateTmpZkPathStepMeta) meta;
	}

	@Override
	protected int handle() {
		if (meta.getVariable("isMainNode") != null) {
			meta.log("INFO","当前进程为主进程！");
			return StepState.RUNNING_SUCCESSED;
		} else{
			ZKUtil zkutil = ZKUtil.getInstance(DataCollectionUtils.getProperties("zookeeper.connect"));
			String zkPath=meta.getZkPath();
			Stat stat = zkutil.exists(zkPath);
			
			if(stat != null)
			{
				meta.log("INFO","已存在路径{}！",meta.getZkPath());
				return StepState.RUNNING_FAILED;
			}else{
				meta.log("INFO","路径{}不存在，进行创建！",meta.getZkPath());
				int result = zkutil.createE(zkPath, meta.getData());
				if (result == ZKUtil.create_ok) {
					
					meta.getStepState().getReturnValue().put("isMainNode", "true");
					return StepState.RUNNING_SUCCESSED;
				} else
				
					return StepState.RUNNING_FAILED;
			
			}
		
		}
	}

	@Override
	public boolean stop() {
		// TODO Auto-generated method stub
		return false;
	}

}
